﻿using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryMIS2019
{
    public partial class Library : Form
    {
        public string userName;
        public string pwe;
        string threshoid;//阈值
        public Library()
        {
            InitializeComponent();
        }    
        private void PassWoreEvent()
        {
            switch (threshoid )
            {
                case "Enter":
                    if (this.tbPassWore.Text == "请输入密码")
                    {
                        this.tbPassWore.PasswordChar = '*';
                        this.tbPassWore.Clear();//清除文本框
                    }
                    break;
                case "Leave":
                    if (this.tbPassWore.TextLength == 0)
                    {
                        this.tbPassWore.PasswordChar = default  (char);
                        this.tbPassWore.Text = "请输入密码";
                    }                
                    break;
            }
            if (this.tbPassWore.TextLength != 0 || this.tbPassWore.Text != "请输入密码")
            {
                this.tbPassWore.PasswordChar = '*';
                pwe = this.tbPassWore.Text.Trim();
            }
        }        
        private void UserNameEvent()
        {
            switch (threshoid )
            {
                case "Enter":
                    if (this.tbUserName.Text == "请输入账号")
                        this.tbUserName.Clear();//清除文本框
                    break;
                case "Leave":
                    if (this.tbUserName.TextLength == 0)
                        this.tbUserName.Text ="请输入账号";
                    break;
            }
            if (this.tbUserName.TextLength != 0 || this.tbUserName.Text != "请输入账号")
                userName = this.tbUserName.Text.Trim(); 
        }
        private void tbUserName_Click(object sender, EventArgs e)
        {
            UserNameEvent(); 
        }
        private void tbPassWore_Click(object sender, EventArgs e)
        {
            PassWoreEvent();
        }
        /// <summary>
        /// 取消按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ClearText();//调用cleartext（）方法清空文本框的内容
            this.tbPassWore.PasswordChar = default(char);
            this.tbUserName.Text = "请输入账号";
            this.tbPassWore.Text = "请输入密码";
        }
        private void tbUserName_Leave(object sender, EventArgs e)
        {
            threshoid = "Leave";
            UserNameEvent(); 
        }
        private void tbPassWore_Leave(object sender, EventArgs e)
        {
            threshoid = "Leave";
            PassWoreEvent();
        }
        /// <summary>
        /// 清除当前窗体中的文本框的内容
        /// </summary>
        private void ClearText()
        {
            foreach (Control Ctrol in this .Controls )
            {
                if (Ctrol is TextBox )
                {
                    Ctrol.Text = "";
                }
            }
        }
        private void tbPassWore_TextChanged(object sender, EventArgs e)
        {

        }
        private void tbPassWore_Enter(object sender, EventArgs e)
        {
            threshoid = "Enter";
            PassWoreEvent();
        }
        private void tbUserName_Enter(object sender, EventArgs e)
        {
            threshoid = "Enter";
            UserNameEvent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (userName.Length ==0)
            {
                MessageBox.Show("请输入用户名!", "系统提示");
                this.tbPassWore.Focus();
                return;
            }
            if (pwe.Length==0)
            {
                MessageBox.Show("请输入密码！", "系统提示");
                this.tbPassWore.Focus();
                return;
            }
            if (userName .Length ==0&&pwe .Length ==0)
            {
                MessageBox.Show("用户或密码不能为空！", "系统提示");
                return;
            }
            BusinessLogic logic = new BusinessLogic();//实例化
            if (logic .UserLogin (userName ,pwe ))
            {
                MessageBox.Show("恭喜你，你已经登录成功！", "系统提示");
               
            }
            else
            {
                MessageBox.Show("用户名或密码错误！", "请验证后重新登录！");
            }
        }
    }
}
